package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.constants.CommonConstants;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanMedia;
import com.xuecheng.content.mappers.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.media.client.MediaClient;
import com.xuecheng.web.exceptions.BusinessException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-16
 */
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {

    @Autowired
    private MediaClient mediaClient;
    @Autowired
    private TeachplanService teachplanService;
    @Autowired
    private CourseBaseService courseBaseService;

    @Override
    public ResponseResult association(BindTeachplanMediaDto dto) {
        Long mediaId = dto.getMediaId();
        Long teachplanId = dto.getTeachplanId();
        if (ObjectUtils.isEmpty(mediaId) || ObjectUtils.isEmpty(teachplanId)){
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        Teachplan teachplan = teachplanService.getById(teachplanId);
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        if (CommonConstants.DELETE_FLAG.equals(courseBase.getStatus())) {
            throw new BusinessException(ErrorCode.NOTFOUND);
        }
        String auditStatus = courseBase.getAuditStatus();
        if (CourseConstants.AUDIT_PUBLISHED_STATUS.equals(auditStatus) ||
                CourseConstants.AUDIT_PASTED_STATUS.equals(auditStatus) ||
                CourseConstants.AUDIT_COMMIT_STATUS.equals(auditStatus)) {
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        ResponseResult<MediaVo> response = mediaClient.findMediaById(mediaId);
        Integer code = response.getCode();
        if (code != ErrorCode.SUCCESS.getCode()){
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        MediaVo media = response.getData();
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId, teachplan.getId());

        TeachplanMedia teachplanMedia = this.getOne(queryWrapper);

        if (ObjectUtils.isEmpty(teachplanMedia)) {
            teachplanMedia = new TeachplanMedia();
            teachplanMedia.setTeachplanId(teachplan.getId());
            teachplanMedia.setMediaId(media.getId());
            teachplanMedia.setMediaFilename(media.getFilename());
            teachplanMedia.setCourseId(courseBase.getId());
            boolean result = this.save(teachplanMedia);
            if (!result) {
                throw new BusinessException(ErrorCode.ERROR);
            }
        } else {
            teachplanMedia.setMediaId(media.getId());
            teachplanMedia.setMediaFilename(media.getFilename());
            boolean result = this.updateById(teachplanMedia);
            if (!result) {
                throw new BusinessException(ErrorCode.ERROR);
            }
        }
        return ResponseResult.okResult();

    }
}
